package com.question.business.dict.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.question.business.dict.model.dto.DictCacheDTO;
import com.question.business.dict.repository.IDictCacheRepository;
import com.question.common.annotation.DataIgnoreConvert;
import com.question.common.annotation.DictConvert;
import com.question.common.annotation.DictIgnoreConvert;
import com.question.common.enums.DataIgnoreConvertEnum;
import com.question.common.model.dto.DataTypeConvertDTO;
import com.question.common.service.IDataConvert;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Objects;

/**
 * 字典转换实现类
 *
 * @author star
 * @since 2023/9/11
 */
@Slf4j
@Service
@AllArgsConstructor
public class DictDataConvertImpl implements IDataConvert {

    private final IDictCacheRepository dictCacheRepository;

    /**
     * 数据转换
     *
     * @param sourceFieldName 源属性
     * @param annotation      注解信息
     * @param value           源数据
     * @return String
     */
    @Override
    public String convert(String sourceFieldName, Annotation annotation, Object value) {
        if (Objects.nonNull(value)) {
            DictConvert dictConvert = (DictConvert) annotation;
            Map<String, DictCacheDTO> map = dictCacheRepository.queryMapByType(StringUtils.isBlank(dictConvert.type()) ? sourceFieldName : dictConvert.type());
            if (CollectionUtils.isNotEmpty(map)) {
                DictCacheDTO dto = map.get(value);
                if (Objects.nonNull(dto) && StringUtils.isNotBlank(dto.getDictDesc())) {
                    return dto.getDictDesc();
                }
            }
        }
        return null;
    }

    @Override
    public DataTypeConvertDTO confirmType(Field field) {
        DictConvert dictConvert = field.getAnnotation(DictConvert.class);
        if (Objects.nonNull(dictConvert)) {
            String targetFieldName = dictConvert.target();
            if (StringUtils.isBlank(targetFieldName)) {
                targetFieldName = field.getName().concat("Desc");
            }
            return DataTypeConvertDTO.builder()
                    .annotation(dictConvert)
                    .dataConvert(this)
                    .targetField(targetFieldName)
                    .build();
        }
        return null;
    }

    /**
     * 数据转换忽略
     *
     * @param ignoreEnum  方法注解信息
     * @param sourceField 字段信息
     * @return boolean
     */
    @Override
    public boolean isIgnore(DataIgnoreConvertEnum ignoreEnum, Field sourceField) {
        DataIgnoreConvert dataIgnoreConvert = sourceField.getAnnotation(DataIgnoreConvert.class);
        boolean isDataIgnore = Objects.nonNull(dataIgnoreConvert) && DataIgnoreConvertEnum.DICT.equals(dataIgnoreConvert.type());
        DictIgnoreConvert dictIgnoreConvert = sourceField.getAnnotation(DictIgnoreConvert.class);
        return DataIgnoreConvertEnum.DICT.equals(ignoreEnum) || isDataIgnore || Objects.nonNull(dictIgnoreConvert);
    }

}
